﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HRProject.Code.DAO;
using HRProject.Code.Common;
using HRProject.Code.DTO;
using System.IO;
using System.Globalization;

namespace HRProject.Asset
{
    public partial class AddAssetType : System.Web.UI.Page
    {
        private Guid parentTypeId = Guid.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            string parentId = Request.Params["parentId"];
            try
            {
                parentTypeId = new Guid(parentId);
            }
            catch (Exception exp)
            {
                Console.Out.Write(exp.StackTrace);
                parentTypeId = Guid.Empty;
            }
            string parentName = Request.Params["parentName"];
            tbxParent.Text = parentName;
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (!BLAuthentication.IsAddAllowed)
            {
                lblError.Text = ContentProvider.GetMessage("NotAuthorized");
                return;
            }

            BLAssetType assetType = new BLAssetType();
            BLAssetType parent = new BLAssetType();
            assetType.Name = tbxName.Text;
            parent.Id = parentTypeId;
            assetType.Parent = parent;
            assetType.Description = txaDescription.Text;
            assetType.DataSignature = 0;

            string iconUrl = string.Empty;
            
            AssetTypeDAO dao = new AssetTypeDAO();
            assetType.Id = Guid.NewGuid();
            int errorCode = 0;
            int result = dao.Insert(assetType, ref errorCode);

            string[] assetNames = Request.Params.GetValues("assetName");
            string[] assetDescriptions = Request.Params.GetValues("assetDescription");
            string[] assetIconUrls = Request.Params.GetValues("assetImageUrl");
            string[] assetProductIds = Request.Params.GetValues("assetProductId");
            string[] assetImportDates = Request.Params.GetValues("assetImportDate");
            string[] assetTerminateDates = Request.Params.GetValues("assetTerminateDate");

            AssetDAO assetDAO = new AssetDAO();
            for (int i = 0; i < assetNames.Length; i++)
            {
                BLAsset asset = new BLAsset();
                asset.Name = assetNames[i];
                asset.Type = assetType;
                asset.ProductId = assetProductIds[i];
                asset.Description = assetDescriptions[i];
                asset.ImageUrl = assetIconUrls[i];
                DateTime date;
                if (DateTime.TryParseExact(assetImportDates[i], Constant.DateTimeFormat, Constant.enCulture, DateTimeStyles.None, out date))
                {
                    asset.ImportDate = date;
                }
                if (DateTime.TryParseExact(assetTerminateDates[i], Constant.DateTimeFormat, Constant.enCulture, DateTimeStyles.None, out date))
                {
                    asset.TerminateDate = date;
                }
                int result2 = assetDAO.insert(asset);
            }

            Util.CloseFrame(Response);
        }
    }
}
